<?php
// This file is part of Meta AGM grading method - https://code.google.com/p/oasychev-moodle-plugins
//
// Meta AGM grading method is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Meta AGM is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Meta AGM.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Meta AGM manager form.
 *
 * @package    gradingform_metaagm
 * @author     Nikita Kalinin <nixorv@gmail.com>
 * @copyright  2014 Oleg Sychev (Volgograd State Technical University) <oasychev@gmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace gradingform_metaagm\view;

defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir . '/formslib.php');

/**
 * Meta AGM manager form class.
 *
 * @package    gradingform_metaagm
 * @author     Nikita Kalinin <nixorv@gmail.com>
 * @copyright  2014 Oleg Sychev (Volgograd State Technical University) <oasychev@gmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class form_metacriterion extends \moodleform {
    
    function definition() {
        global $PAGE, $OUTPUT;
        
        $mform = $this->_form;
        
        if (!isset($this->_customdata['metacriterion'])) {
            throw new coding_exception('Metacriterion wasn\'t passed.');
        }
        $mc = $this->_customdata['metacriterion'];
        
        $mform->addElement('hidden', 'metacriterionid', $mc->id);
        $mform->setType('metacriterionid', PARAM_INT);
        
        $mform->addElement('header', 'metacriterion' . $mc->id, $mc->name);
        
        $mform->addElement('static', 'weight', get_string('metacriterionweight', 'gradingform_metaagm'), $mc->weight);
        
        $manager = get_grading_manager($mc->subareaid);
        $controller = $manager->get_controller($mc->activemethod);
        
        $formcontent = '';
        
        // Display the message if the form is currently not available (if applicable).
        if ($controller->form_unavailable_notification()) {
            $formcontent .= $OUTPUT->notification(get_string('agmnotready', 'gradingform_metaagm'));
        }
        // Display the grading form preview.
        if ($controller->is_form_defined()) {
            $formcontent .= $controller->render_preview($PAGE);
        }
        $mform->addElement('static', 'gradingform', get_string('gradingform', 'gradingform_metaagm'), $formcontent);
        
        // Action buttons for current metacriterion.
        $buttons = array();
        if ($controller->is_form_defined()) {
            $buttons[] = $mform->createElement('submit', 'editform', get_string('editform', 'gradingform_metaagm'));
            if ($this->is_form_share_allowed($controller)) {
                $buttons[] = $mform->createElement('submit', 'publishform', get_string('publishform', 'gradingform_metaagm'));
            }
            $buttons[] = $mform->createElement('submit', 'rmform', get_string('deleteform', 'gradingform_metaagm'));
        } else {
            $buttons[] = $mform->createElement('submit', 'newdef', get_string('definenew', 'gradingform_metaagm'));
            $buttons[] = $mform->createElement('submit', 'deftmpl', get_string('definefromtemplate', 'gradingform_metaagm'));
        }
        $buttons[] = $mform->createElement('submit', 'editmc', get_string('editsettings'));
        $buttons[] = $mform->createElement('submit', 'rm', get_string('deletemetacriterion', 'gradingform_metaagm'));
        $mform->addGroup($buttons);
    }

    /**
     * @param \gradingform_controller $controller
     * @return bool Is form sharing allowed.
     */
    private function is_form_share_allowed(\gradingform_controller $controller) {
        global $DB;

        if (!has_capability('moodle/grade:sharegradingforms', \context_system::instance())) {
            return false;
        }

        if (!$controller->is_form_defined()) {
            return false;
        }

        $definition = $controller->get_definition();
        if (empty($definition->copiedfromid)) {
            $hasoriginal = false;
        } else {
            $hasoriginal = $DB->record_exists('grading_definitions', array('id' => $definition->copiedfromid));
        }
        if (!$controller->is_form_available()) {
            // Drafts can not be shared.
            return false;
        } else if (!$hasoriginal) {
            // Was created from scratch or is orphaned.
            if (empty($definition->timecopied)) {
                // Was never shared before.
                return true;
            } else if ($definition->timemodified > $definition->timecopied) {
                // Was modified since last time shared.
                return true;
            } else {
                // Was not modified since last time shared.
                return false;
            }
        } else {
            // Was created from a template and the template still exists.
            if ($definition->timecreated == $definition->timemodified) {
                // Was not modified since created.
                return false;
            } else if (empty($definition->timecopied)) {
                // Was modified but was not re-shared yet.
                return true;
            } else if ($definition->timemodified > $definition->timecopied) {
                // Was modified since last time re-shared.
                return true;
            } else {
                // Was not modified since last time re-shared.
                return false;
            }
        }
    }
}
